package tech.abing.spider.task.pageprocessor.amac;

import java.sql.Timestamp;
import java.util.List;
import java.util.regex.Pattern;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.amac.FundAccountEntity;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

public class FundAccountDetailProcessor implements PageProcessor{
	
	private static Logger logger = Logger.getLogger(FundAccountDetailProcessor.class);
	
	private static int record = 0;

	@Override
	public PageScript getJavaScript() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context,
			List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		Document doc = Jsoup.parse(page.getContent());
		record++;
		logger.info("正在解析第"+record+"条数据:");
		FundAccountEntity fa = (FundAccountEntity)page.getRequest().getExtParams().get("fa");
		Element table = doc.select("table[class=\"table table-center table-info\"] > tbody").first();
		fa.setBailor(table.getElementsMatchingOwnText("托管人名称").first().nextElementSibling().text());
		fa.setName(table.getElementsMatchingOwnText("专户名称").first().nextElementSibling().text());
//		fa.setContractDeadline(table.getElementsMatchingOwnText("合同期限（月）").first().nextElementSibling().text());
		String scaleOfCreated = table.getElementsMatchingOwnText("设立时募集资金总额（万元）").first().nextElementSibling().text();
		if(StringUtils.isNotEmpty(scaleOfCreated)){
			fa.setScaleOfCreated(Double.parseDouble(scaleOfCreated));
		}
		fa.setHasClassify(table.getElementsMatchingOwnText("是否分级").first().nextElementSibling().text());
		fa.setInvestNumofCreated(Integer.parseInt(table.getElementsMatchingOwnText("成立时投资者数量").first().nextElementSibling().text()));
//		fa.setProductType(table.getElementsMatchingOwnText("非专项资产管理计划产品类型").first().nextElementSibling().text());
		fa.setCreateTime(new Timestamp(System.currentTimeMillis()));
		fa.setId(UuIdPublic.timeStrongID(5));
		StringUtils.removeSpace(fa);
		objectContainer.add(fa);
		
//		FundAccountEntity fa = (FundAccountEntity)page.getRequest().getExtParams().get("fa");
//		Element table = doc.select("table[class=\"table table-center table-info\"] > tbody").first();
//		fa.setBailor(table.getElementsMatchingOwnText("托管人名称").first().nextElementSibling().text());
//		fa.setContractDeadline(table.getElementsMatchingOwnText("合同期限（月）").first().nextElementSibling().text());
//		String scaleOfCreated = table.getElementsMatchingOwnText("起始规模（亿元）").first().nextElementSibling().text();
//		if(StringUtils.isNotEmpty(scaleOfCreated)){
//			fa.setScaleOfCreated(Double.parseDouble(scaleOfCreated));
//		}
//		fa.setHasClassify(table.getElementsMatchingOwnText("是否分级").first().nextElementSibling().text());
//		fa.setInvestNumofCreated(Integer.parseInt(table.getElementsMatchingOwnText("成立时投资者数量").first().nextElementSibling().text()));
//		fa.setProductType(table.getElementsMatchingOwnText("非专项资产管理计划产品类型").first().nextElementSibling().text());
//		fa.setCreateTime(new Timestamp(System.currentTimeMillis()));
//		fa.setId(UuIdPublic.timeStrongID(5));
//		StringUtils.removeSpace(fa);
//		objectContainer.add(fa);
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
